package com.imooc.controller;

import com.imooc.pojo.Orders;
import com.imooc.pojo.Users;
import com.imooc.pojo.vo.UsersVO;
import com.imooc.service.center.MyOrdersService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.RedisOperator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.UUID;

@Controller
public class BaseController {
    @Autowired
    private RedisOperator redisOperator;
    public static final String FOODIE_SHOPCART="shopcart";
    public static final Integer COMMON_PAGE_SIZE=10;
    public static final Integer PAGE_SIZE=20;
    public static final String REDIS_USER_TOKEN="redis_user_token";

    //支付中心的调用地址
    String paymentUrl = "http://payment.t.mukewang.com/foodie-payment/payment/createMerchantOrder";

    //微信支付成功->支付中心->天天吃货平台
    //                                    下方是回调通知url
    public String payReturnUrl="http://host.haowangzhan.top/foodie-dev-api/orders/notifyMerchantOrderPaid";


    //用户上传头像的位置（后续不使用这种方式，改为properties文件关联注入）
    public static final String IMAGE_USER_FACE_LOCATION= "F:"+File.separator+
                                                        "jianguoyun"+File.separator+
                                                        "java_code"+File.separator+
                                                        "faces";

    @Autowired
    public MyOrdersService myOrdersService;


    /**
     * 用于验证用户和订单是否有关联，避免非法用户调用
     * @param orderId
     * @param userId
     * @return
     */
    public IMOOCJSONResult checkUserOrder(String userId, String orderId){
        Orders order = myOrdersService.queryMyOrder(userId, orderId);
        if(order==null){
            return IMOOCJSONResult.errorMsg("订单不存在");
        }
        return IMOOCJSONResult.ok(order);
    }

    /**
     * 实现用户信息token，且返回的usersVO中只包含必要信息，保证信息
     * @param userResult
     * @return
     */
    public UsersVO convertUserVO(Users userResult){
        //实现用户的redis会话
        String uniqueToken= UUID.randomUUID().toString().trim();
        redisOperator.set(REDIS_USER_TOKEN+":"+userResult.getId(), uniqueToken);

        UsersVO usersVO=new UsersVO();
        BeanUtils.copyProperties(userResult,usersVO);
        usersVO.setUserUniqueToken(uniqueToken);

        return usersVO;
    }
}
